<?php

namespace app\admin\model\shop;

use think\Model;
use think\Cache;
class Contract extends Model
{
    // 表名
    protected $name = 'shop_contract';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'type_text',
        'gender_text',
        'edition_text',
        'status_text',
        'province_text',
        'city_text',
        'area_text',
        'closing_date_text',
        'sign_date_text',
        'z_publish_text',
    ];
    

       /**
     * 获取省的名称
     * @return array
     */
    public function getProvinceOrderList()
    {
        if(!$list = \think\Cache::get('arealist'))
        {
            $area = model(GetTableModel('Area'))::field('id,name')->select();
            $area = collection($area)->toArray();
            $list = array_column($area,'name','id');
            Cache::set('areaList',$list,0);//设置0永久缓存不过期
        }
        return $list;
    }
       /**
     * 获取省的名称
     * @return array
     */
    public function getProvinceOrderShortList()
    {

        if(!$list = \think\Cache::get('arealistsort'))
        {
            $area = model(GetTableModel('Area'))::field('id,shortname')->select();
            $area = collection($area)->toArray();
            $list = array_column($area,'shortname','id');
            Cache::set('arealistsort',$list,0);//设置0永久缓存不过期
        }
        return $list;
    }
     public function getProvinceTextAttr($value, $data)
    {
        $value = $value ? $value : $data['province'];
        $list = $this->getProvinceOrderList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCityTextAttr($value, $data)
    {
        $value = $value ? $value : $data['city'];
        $list = $this->getProvinceOrderList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getAreaTextAttr($value, $data)
    {
        $value = $value ? $value : $data['area'];
        $list = $this->getProvinceOrderList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getTypeList()
    {
        // return ['1' => __('Type 1'),'2' => __('Type 2'),'3' => __('Type 3')];
         return ['1' => __('Type 1')];
    }     

    public function getGenderList()
    {
        return ['1' => __('Gender 1'),'2' => __('Gender 2')];
    }     

    public function getEditionList()
    {
        return ['1' => __('Edition 1'),'2' => __('Edition 2'),'3' => __('Edition 3'),'4' => __('Edition 4'),'5' => __('Edition 5'),'6' => __('Edition 6'),'7' => __('Edition 7')];
    }     

    public function getStatusList()
    {
        return ['1' => __('Statuss 1'),'2' => __('Statuss 2')];
    }     


    public function getTypeTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['type'];
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getGenderTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['gender'];
        $list = $this->getGenderList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getEditionTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['edition'];
        $list = $this->getEditionList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getClosingDateTextAttr($value, $data)
    {
        $value = $value ? $value : $data['closing_date'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSignDateTextAttr($value, $data)
    {
        $value = $value ? $value : $data['sign_date'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setClosingDateAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    } 
    protected function setSignDateAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }
     public function getZPublishTextAttr($value, $data)
    {
   
        $value = $value ? $value : $data['z_publish'];
         if(!$userlist = Cache::get('USERLIST'))
        {
             $userlist = collection(model(GetTableModel('Admin'))::field("userid,username,gender,isleader,department,delete_status")->select())->toArray();
            $userlist = array_column($userlist,null,'userid');
            Cache::set('USERLIST',$userlist,604800);//有效期一个星期
        }
        return isset($userlist[$value]) ? $userlist[$value]['username']:$value.' 已离职';
    }
    protected function setZPublishAttr($value)
    {
        return \think\Session::get('admin')['userid'];
    } 


}
